package com.jbookshelf.dal.providers;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jbookshelf.dal.entities.Orders;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class OrdersDataProviderImpl implements OrdersDataProvider {

	@Autowired
	SessionFactory sessionFactory;

	@Override
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
	public List<Orders> getOrders() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Orders ord join fetch ord.users join fetch ord.shops join fetch ord.orderStatuses");
		return query.list();
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveOrder(Orders order) {
		sessionFactory.getCurrentSession().save(order);
	}


}
